var orderTimer;
var payToUrl = '';

// 购买成功添加事件绑定
$('#buySuccessModal')[0].addEventListener('show.bs.modal', showBuySuccessModelEvent)
$(document).ready(function () {
    getQrCode();
});

// 展示购买成功模态框事件
function showBuySuccessModelEvent() {
    let timeOut = 3
    let timer = setInterval(function () {
        $('#timeOut').text(timeOut)
        if (timeOut == 0) {
            clearInterval(timer)
            if(payToUrl){
                location.href = payToUrl;
            }else{
                window.history.go(-1);
            }
            return
        }
        --timeOut
    }, 1000)
}

function getQrCode() {
    var orderNo = getUrlParam('order_no');
    if (orderNo) {
        let token = localStorage.getItem(TOKEN)
        $.ajax({
            url: "/api/order/qrcode",
            type: "GET",
            data: {
                order_no: orderNo
            },
            beforeSend: function (xhr) {
                xhr.setRequestHeader("Authorization", "Bearer " + token);
            },
            success: function (res) {
                if (res.code == 200) {
                    $('.payCode').hide()
                    $("#payQrCode").attr("src", res.data.data);
                    var money = formatCurrency(res.data.order_info.amount.total / 100);
                    $(".card-header span").html(money);
                    $(".card-header p").eq(1).html("订单号:" + res.data.order_info.out_trade_no);
                    $(".card-header p").eq(2).html("购买商品:" + res.data.order_info.description);
                    var intDiff = parseInt(60);
                    timer(intDiff);
                }
            }
        });
    }
    // 倒计时
}

function timer(intDiff) {
    orderTimer = window.setInterval(function () {
        var day = 0,
            hour = 0,
            minute = 0,
            second = 0;//时间默认值
        if (intDiff > 0) {
            day = Math.floor(intDiff / (60 * 60 * 24));
            hour = Math.floor(intDiff / (60 * 60)) - (day * 24);
            minute = Math.floor(intDiff / 60) - (day * 24 * 60) - (hour * 60);
            second = Math.floor(intDiff) - (day * 24 * 60 * 60) - (hour * 60 * 60) - (minute * 60);
            var orderNo = getUrlParam('order_no');
            $.ajax({
                url: "/api/order/checkStatus",
                type: "GET",
                data: {
                    order_no: orderNo
                },
                success: function (res) {
                    if (res.data.status == 1) {
                        timeOut()
                        if(res.data.type == 4){
                             payToUrl ='/download?out_trade_no='+orderNo;
                        }else{
                             payToUrl = '';
                        }
                        $('#buySuccessModal').modal('show')
                    }
                }
            });
        } else if (intDiff === 0) {
            timeOut()
        }
        if (minute <= 9) minute = '0' + minute;
        if (second <= 9) second = '0' + second;
        $('#day_show').html(day + "天");
        $('#hour_show').html('<s id="h"></s>' + hour + '时');
        $('#minute_show').html('<s></s>' + minute + '分');
        $('#second_show').html('<s></s>' + second + '秒');
        intDiff--;
    }, 1000);
}

// 倒计时过期
function timeOut() {
    $('.payCode').show()
    clearInterval(orderTimer)
}

